package com.xmim.core.container;

import com.xmim.core.container.handler.AbstractHandlerManager;
import com.xmim.core.exception.ContainerException;

public abstract class AbstractContainer implements Container {
    protected final ContainerFactory factory;
    protected final Container parent;
    protected boolean connect;

    public AbstractContainer(ContainerFactory factory, Container parent) {
        this.factory = factory;
        this.parent = parent;
        this.onInit();
    }


    @Override
    public synchronized AbstractContainer start() throws ContainerException {
        if (this.parent != null) {
            this.parent.start();
        }
        this.onStart();
        this.connect = true;
        return this;
    }


    @Override
    public synchronized AbstractContainer close() throws ContainerException {
        if (this.parent != null) {
            this.parent.close();
        }
        this.onClose();
        this.connect = false;
        return this;
    }

    /**
     * 初始化
     */
    protected abstract void onInit();

    /**
     * 启动操作
     */
    protected abstract void onStart() throws ContainerException;

    /**
     * 关闭操作
     */
    protected abstract void onClose() throws ContainerException;


    @Override
    public boolean isConnect() {
        return connect;
    }

    @Override
    public Container getParent() {
        return parent;
    }

    @Override
    public ContainerFactory getFactory() {
        return factory;
    }
}
